package com.czk.gulimall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czk.common.to.MemberPrice;
import com.czk.common.to.SkuReduction;
import com.czk.common.utils.PageUtils;
import com.czk.common.utils.Query;
import com.czk.gulimall.coupon.dao.SkuFullReductionDao;
import com.czk.gulimall.coupon.entity.MemberPriceEntity;
import com.czk.gulimall.coupon.entity.SkuFullReductionEntity;
import com.czk.gulimall.coupon.entity.SkuLadderEntity;
import com.czk.gulimall.coupon.service.MemberPriceService;
import com.czk.gulimall.coupon.service.SkuFullReductionService;
import com.czk.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    @Autowired
    private SkuLadderService skuLadderService;
    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReduction skuReduction) {
        // 1 保存满减打折，会员价
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuReduction.getSkuId());
        skuLadderEntity.setFullCount(skuReduction.getFullCount());
        skuLadderEntity.setDiscount(skuReduction.getDiscount());
        skuLadderEntity.setAddOther(skuReduction.getCountStatus());
        skuLadderService.save(skuLadderEntity);

        // 保存满减信息
        SkuFullReductionEntity reductionEntity = new SkuFullReductionEntity();
        // 属性拷贝
        BeanUtils.copyProperties(skuReduction, reductionEntity);
        this.save(reductionEntity);
        // 会员价格
        List<MemberPrice> memberPrice = skuReduction.getMemberPrice();
        //
        List<MemberPriceEntity> collect = memberPrice.stream().map(item -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuReduction.getSkuId());
            // 会员等级id
            memberPriceEntity.setMemberLevelId(item.getId());
            // 会员名字
            memberPriceEntity.setMemberLevelName(item.getName());
            // 会员价格
            memberPriceEntity.setMemberPrice(item.getPrice());
            // 是否叠加
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(collect);
    }
}